﻿using Dapper;
using System.Collections.Generic;
using System.Data;
using System.Threading.Tasks;
using BCCommon;
using BCEntity.TMS.MoveHouseConfig.MoveHouseSpecificationConfigOverWrite;
using BCEntity.TMS.MoveHouseConfig.MoveHouseSpecificationConfig;

namespace BCData.TMS.MoveHouseConfig.MoveHouseSpecificationConfigOverWrite
{
    public class MoveHouseSpecificationConfigOverWriteData : IMoveHouseSpecificationConfigOverWriteData
    {
        private readonly IDatabaseContext databaseContext;
        private readonly string QUERY = @"SELECT
		    A.MoveHouseSpecificationConfigOverWriteId,
		    A.MoveHouseSpecificationConfigId,
		    A.SpecificationName,
		    A.SpecificationPinYin,
            A.MoveHouseConfigId,
            A.MoveHouseConfigName,
		    A.ProvinceCode,
		    A.CityCode,
		    A.CountyCode,
		    A.AgentRecordId,
		    A.Price,
		    A.Remark,
		    A.Enabled,
		    A.CreateAdminId,
		    A.CreateAdminName,
		    A.CreateTime,
		    A.UpdateTime,
		    A.EditAdminId,
		    A.EditAdminName
        FROM
            movehousespecificationconfigoverwrite A ";



        #region 构造函数
        public MoveHouseSpecificationConfigOverWriteData(IDatabaseContext databaseContext)
        {
            this.databaseContext = databaseContext;
        }
        #endregion

        #region InsertAsync
        /// <summary>
        /// 添加新搬家规格配置重写纪录
        /// </summary>
        /// <param name="moveHouseSpecificationConfigOverWriteEntity"></param>
        /// <returns></returns>
        public async Task<MoveHouseSpecificationConfigOverWriteEntity> InsertAsync(MoveHouseSpecificationConfigOverWriteEntity entity)
        {
            return await this.databaseContext.Master.InsertAsync(entity);
        }
        #endregion

        #region updateAsync
        /// <summary>
        /// 修改搬家规格配置重写记录
        /// </summary>
        /// <param name="moveHouseSpecificationConfigOverWriteEntity"></param>
        /// <returns></returns>
        public async Task<MoveHouseSpecificationConfigOverWriteEntity> UpdateAsync(MoveHouseSpecificationConfigOverWriteEntity entity)
        {
            return await this.databaseContext.Master.UpdateAsync(entity);
        }
        #endregion 

        #region Delete
        /// <summary>
        /// 删除搬家规格配置重写记录
        /// </summary>
        /// <param name="moveHouseSpecificationConfigOverWriteId"></param>
        /// <returns></returns>
        public async Task<bool> DeleteAsync(long moveHouseSpecificationConfigOverWriteId)
        {
            string sql = "DELETE FROM movehousespecificationconfigoverwrite WHERE MoveHouseSpecificationConfigOverWriteId = ?moveHouseSpecificationConfigOverWriteId";
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?MoveHouseSpecificationConfigOverWriteId", moveHouseSpecificationConfigOverWriteId, DbType.Int64);
            return await this.databaseContext.Master.ExecuteAsync(sql, parameters, databaseContext.Transaction) > 0;
        }
        #endregion

        #region GetById
        /// <summary>
        /// 获取搬家规格配置重写记录
        /// </summary>
        /// <param name="moveHouseSpecificationConfigOverWriteId"></param>
        /// <returns></returns>
        public async Task<MoveHouseSpecificationConfigOverWriteEntity> GetEntityAsync(long moveHouseSpecificationConfigId,
            string provinceCode, string cityCode, string countyCode, long agentRecordId)
        {
            DynamicParameters parameters = new DynamicParameters();
            List<string> wheres = new List<string>();
            if (!string.IsNullOrWhiteSpace(provinceCode))
            {
                wheres.Add(" A.ProvinceCode=?ProvinceCode ");
                parameters.Add("?ProvinceCode", provinceCode, DbType.AnsiString);
            }
            if (!string.IsNullOrWhiteSpace(cityCode))
            {
                wheres.Add(" A.CityCode=?CityCode ");
                parameters.Add("?CityCode", cityCode, DbType.AnsiString);
            }
            if (!string.IsNullOrWhiteSpace(countyCode))
            {
                wheres.Add(" A.CountyCode=?CountyCode ");
                parameters.Add("?CountyCode", countyCode, DbType.AnsiString);
            }
            wheres.Add(" A.MoveHouseSpecificationConfigId=?MoveHouseSpecificationConfigId ");
            parameters.Add("?MoveHouseSpecificationConfigId", moveHouseSpecificationConfigId, DbType.Int64);
            wheres.Add(" A.AgentRecordId=?AgentRecordId ");
            parameters.Add("?AgentRecordId", agentRecordId, DbType.Int64);
            var sqlWhere = wheres.Count > 0 ? " WHERE " + string.Join(" AND ", wheres) : "";
            string sql = QUERY + sqlWhere;
            return await this.databaseContext.Master.QueryFirstOrDefaultAsync<MoveHouseSpecificationConfigOverWriteEntity>(sql, parameters);
        }
        #endregion



        #region GetById
        /// <summary>
        /// 获取搬家规格配置重写记录
        /// </summary>
        /// <param name="moveHouseSpecificationConfigOverWriteId"></param>
        /// <returns></returns>
        public async Task<MoveHouseSpecificationConfigOverWriteEntity> GetEntityAsync(long moveHouseSpecificationConfigOverWriteId)
        {
            string sql = QUERY + " WHERE MoveHouseSpecificationConfigOverWriteId = ?moveHouseSpecificationConfigOverWriteId";

            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?MoveHouseSpecificationConfigOverWriteId", moveHouseSpecificationConfigOverWriteId, DbType.Int64);
            return await this.databaseContext.Master.QueryFirstOrDefaultAsync<MoveHouseSpecificationConfigOverWriteEntity>(sql, parameters);
        }
        #endregion

        public async Task<MoveHouseSpecificationConfigViewEntity> GetEntityViewAsync(long moveHouseSpecificationConfigOverWriteId)
        {
            string sql = QUERY + " WHERE MoveHouseSpecificationConfigOverWriteId = ?moveHouseSpecificationConfigOverWriteId";

            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?MoveHouseSpecificationConfigOverWriteId", moveHouseSpecificationConfigOverWriteId, DbType.Int64);
            return await this.databaseContext.Master.QueryFirstOrDefaultAsync<MoveHouseSpecificationConfigViewEntity>(sql, parameters);
        }

        #region GetListAsync 查询搬家规格配置重写记录
        public async Task<(IEnumerable<MoveHouseSpecificationConfigOverWriteEntity>, int)> GetListAsync(int pageIndex, int pageSize)
        {
            DynamicParameters parameters = new DynamicParameters();
            //查询条件
            List<string> wheres = new List<string>();

            string sql = string.Format(@"{0} {1} ; SELECT COUNT(1) FROM  movehousespecificationconfigoverwrite {1}",
                   QUERY,
                  wheres.Count > 0 ? " WHERE " + string.Join(" AND ", wheres) : "",
                  (pageIndex == 0 ? 0 : pageIndex - 1) * pageSize
                  );

            return await this.databaseContext.QueryMultipleAsync<MoveHouseSpecificationConfigOverWriteEntity, int>(sql, parameters);
        }

        public async Task<IEnumerable<MoveHouseSpecificationConfigOverWriteEntity>> GetList(long moveHouseSpecificationConfigId, bool? isEnable)
        {
            DynamicParameters parameters = new DynamicParameters();
            List<string> wheres = new List<string>();
            if (isEnable.HasValue)
            {
                wheres.Add(" A.Enabled=?Enabled ");
                parameters.Add("?Enabled", isEnable.Value, DbType.Boolean);
            }
            wheres.Add(" A.MoveHouseSpecificationConfigId=?MoveHouseSpecificationConfigId ");
            parameters.Add("?MoveHouseSpecificationConfigId", moveHouseSpecificationConfigId, DbType.Int64);
            var sqlWhere = wheres.Count > 0 ? " WHERE " + string.Join(" AND ", wheres) : "";
            string sql = QUERY + sqlWhere;
            return await this.databaseContext.Slave.QueryAsync<MoveHouseSpecificationConfigOverWriteEntity>(sql, parameters);
        }
        #endregion



    }
}



